﻿using GalaSoft.MvvmLight;
using JiveMessenger.Utilities;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using WebSocket4Net;

namespace JiveMessenger.Network
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class JiveJsonWebSocket : JsonWebSocket
    {
        public JiveJsonWebSocket(string uri)
            : base(uri)
        {
        }
        public JiveJsonWebSocket(WebSocket websocket)
            : base(websocket)
        {
        }
        public JiveJsonWebSocket(string uri, List<KeyValuePair<string, string>> cookies)
            : base(uri, cookies)
        {
        }
        public JiveJsonWebSocket(string uri, string subProtocol)
            : base(uri, subProtocol)
        {
        }
        public JiveJsonWebSocket(string uri, WebSocketVersion version)
            : base(uri, version)
        {
        }
        public JiveJsonWebSocket(string uri, string subProtocol, List<KeyValuePair<string, string>> cookies)
            : base(uri, subProtocol, cookies)
        {
        }
        public JiveJsonWebSocket(string uri, string subProtocol, WebSocketVersion version)
            : base(uri, subProtocol, version)
        {
        }
        public JiveJsonWebSocket(string uri, string subProtocol, List<KeyValuePair<string, string>> cookies, WebSocketVersion version)
            : base(uri, subProtocol, cookies, version)
        {
        }
        public JiveJsonWebSocket(string uri, string subProtocol, List<KeyValuePair<string, string>> cookies, List<KeyValuePair<string, string>> customHeaderItems, string userAgent, WebSocketVersion version)
            : base(uri, subProtocol, cookies, customHeaderItems, userAgent, version)
        {
        }
        public JiveJsonWebSocket(string uri, string subProtocol, List<KeyValuePair<string, string>> cookies, List<KeyValuePair<string, string>> customHeaderItems, string userAgent, string origin, WebSocketVersion version)
            : base(uri, subProtocol, cookies, customHeaderItems, userAgent, origin, version)
        {
        }
        protected override object DeserializeObject(string json, Type type)
        {
            var settings = new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.None,
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
                ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
            };
            settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
            return JsonConvert.DeserializeObject(json, type, settings);
        }
        protected override string SerializeObject(object target)
        {
            var settings = new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.None,
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
                ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
            };
            settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
            string s = JsonConvert.SerializeObject(target, settings);
            return s;
        }
    }
}